//
//  GoodLinkListView.m
//  danpin
//
//  Created by chuangjia on 10/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "GoodLinkListView.h"
#import "CellConfig.h"
#import "viewconfig.h"
#define POPLISTVIEW_SCREENINSET 40.
#define POPLISTVIEW_HEADER_HEIGHT 50.
#define RADIUS 5.

@interface GoodLinkListView (private)
- (void)fadeIn;
- (void)fadeOut;
@end
@implementation GoodLinkListView
@synthesize delegate;
#pragma mark - initialization & cleaning up
- (id)initWithTitle:(NSString *)aTitle options:(NSArray *)aOptions
{
    CGRect rect = CGRectMake(0, DEVICE_HEIGHT/2, DEVICE_WIDTH, DEVICE_HEIGHT/2);
    if (self = [super initWithFrame:rect])
    {
        self.backgroundColor = [UIColor clearColor];
        _title = [aTitle copy];
        _options = [aOptions copy];
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,POPLISTVIEW_HEADER_HEIGHT+PHOTO_FRAME_WIDTH,
                                                                   rect.size.width,
                                                                   rect.size.height - 5 * PHOTO_FRAME_WIDTH - POPLISTVIEW_HEADER_HEIGHT)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self addSubview:_tableView];
        
    }
    return self;
}

- (void)dealloc
{
    _title=nil;
    _options=nil;
    _tableView=nil;
//DLog(@"%s", __PRETTY_FUNCTION__);
}

#pragma mark - Private Methods
- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
}
- (void)fadeOut
{
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

#pragma mark - Instance Methods
- (void)showInView:(UIView *)aView animated:(BOOL)animated
{
    [aView addSubview:self];
    if (animated) {
        [self fadeIn];
    }
}

#pragma mark - Tableview datasource & delegates
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_options count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary *dict =[_options objectAtIndex:indexPath.row];
    return [GoodsLinkCell returnCellHeight:dict];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsLinkCell *cell = (GoodsLinkCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
    if(cell == nil)
    {
        cell = [[GoodsLinkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        __weak typeof (self) wSelf = self;
//        [cell setCallBack:^(BEventType t, UIView *v,id obj,id key) {
//            [wSelf cellAction:t views:v obj:obj key:key];
//        }];
    }
    if (indexPath.row>=[_options count]) {
        return cell;
    }
    NSDictionary *dict =[_options objectAtIndex:indexPath.row];
    [cell showInfo:dict key:@"link" indexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // tell the delegate the selection
    if (self.delegate && [self.delegate respondsToSelector:@selector(linkListView:didSelectedIndex:)]) {
        [self.delegate linkListView:self didSelectedIndex:[indexPath row]];
    }
    
    // dismiss self
    [self fadeOut];
}
#pragma mark - TouchTouchTouch
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // tell the delegate the cancellation
    if (self.delegate && [self.delegate respondsToSelector:@selector(linkListViewDidCancel)]) {
        [self.delegate linkListViewDidCancel];
    }
    
    // dismiss self
    [self fadeOut];
}

#pragma mark - DrawDrawDraw
- (void)drawRect:(CGRect)rect
{
    
}

@end
